package com.atguigu.flink.chapter06;

import com.atguigu.flink.bean.OrderEvent;
import com.atguigu.flink.bean.TxEvent;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.KeyedCoProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashMap;

public class Flink04_Project_Order {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);

        SingleOutputStreamOperator<OrderEvent> orderEventStream = env
                .readTextFile("input/OrderLog.csv")
                .map(line -> {
                    String[] data = line.split(",");
                    return new OrderEvent(
                            Long.valueOf(data[0]),
                            data[1],
                            data[2],
                            Long.valueOf(data[3])
                    );
                })
                .filter(event -> "pay".equals(event.getEventType()));


        SingleOutputStreamOperator<TxEvent> txEventStream = env
                .readTextFile("input/ReceiptLog.csv")
                .map(line -> {
                    String[] data = line.split(",");
                    return new TxEvent(
                            data[0],
                            data[1],
                            Long.valueOf(data[2])
                    );
                });

        // 两个流connect
        orderEventStream
                .connect(txEventStream)
                .keyBy(OrderEvent::getTxId, TxEvent::getTxId)
                .process(new KeyedCoProcessFunction<String, OrderEvent, TxEvent, String>() {

                    HashMap<String, OrderEvent> orderEventMap = new HashMap<>();
                    HashMap<String, TxEvent> txEventMap = new HashMap<>();

                    @Override
                    public void processElement1(OrderEvent orderEvent, Context ctx, Collector<String> out) throws Exception {
                        // 支付信息来了， 判断交易信息是否存在
                        String txId = orderEvent.getTxId();
                        TxEvent txEvent = txEventMap.get(txId);
                        if (txEvent != null) {
                            out.collect("订单:" + orderEvent.getOrderId() + " 对账成功");
                        }else{// 如果对应的交易不存在， 则把支付信息存入到对应的map中
                            orderEventMap.put(txId, orderEvent);
                        }


                    }

                    @Override
                    public void processElement2(TxEvent txEvent, Context ctx, Collector<String> out) throws Exception {
                        String txId = txEvent.getTxId();

                        OrderEvent orderEvent = orderEventMap.get(txId);
                        if (orderEvent != null) {
                            out.collect("订单:" + orderEvent.getOrderId() + " 对账成功");
                        }else{
                            txEventMap.put(txId, txEvent);
                        }
                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
